package com.item.controller;

import com.item.pojo.Picture;
import com.item.pojo.Vehicle;
import com.item.service.PictureService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class PictureController {

    @Resource
    private PictureService pictureService;

    @Resource
    private JmsMessagingTemplate template;

    private final String PATH = "D:\\S4\\item\\nginx-1.21.6\\html\\medical-html\\html\\page\\images\\";

    @RequestMapping("/upload")
    public Map<String, Object> upload(@RequestParam(value = "file") MultipartFile file, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newFileName = uuid + "-" + fileName;
        File filePath = new File(PATH + newFileName);
        Picture picture = new Picture(newFileName, 0);
        try {
            file.transferTo(filePath);
            if (pictureService.addPicture(picture) != 0) {
                session.setAttribute("picture", picture);
                map.put("msg", "图片预览成功");
            }
        } catch (IOException e) {
            map.put("msg", "图片预览失败");
            throw new RuntimeException(e);
        }
        return map;
    }

    private boolean bool = false;

    @RequestMapping("/updatePicture")
    public Map<String, Object> updatePicture(@RequestParam(value = "file") MultipartFile file, HttpSession session, Integer vehicle_image, String imagePath) {
        System.out.println("vehicle_image:" + vehicle_image + "\t\timagePath:" + imagePath);
        Map<String, Object> map = new HashMap<>();
        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newFileName = uuid + "-" + fileName;
        File filePath = new File(PATH + newFileName);
        File file1 = new File(PATH + imagePath);
        try {
            file.transferTo(filePath);
            if (pictureService.saveOrUpdate(new Picture(vehicle_image, newFileName, 0))) {
                map.put("msg", "图片预览成功");
                file1.delete();
            }
        } catch (IOException e) {
            map.put("msg", "图片预览失败");
            throw new RuntimeException(e);
        }
        return map;
    }

//    @GetMapping("/message")
//    public void sendMessage(String message) {
//        template.convertAndSend("message", message);
//    }
//
//    private String result;
//
//
//    @JmsListener(destination = "message")
//    public void getMessage(String message) {
//        result = message;
//        System.out.println("message:" + message);
//    }
}
